<?php

/*

    MMetaverse - Open Metaverse Access Library

    Copyright (c) 2009 Mary Jane Soft - Marlon J. Manrique

    http://www.maryjanesoft.com
    http://www.marlonj.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    Boston, MA  02111-1307  USA

    $Id$

*/

/**
 * @package mjs.mmetaverse.manager
 */

require_once 'MAsset.class.php';
require_once 'MDataBase.class.php';

/**
 * Asset Data Base
 */
class MAssetDB extends MDataBase {
    
    /**
     * Create the database access for the OpenSim object specified
     *
     * @param MMetaverse $metaverse current metaverse
     */
    public function MAssetDB(MMetaverse $metaverse) {
        $this->MDataBase($metaverse);
        $this->db = $this->getDatabase('asset');
    }

    /**
     * Return the image with the uuid specified
     *
     * @param MUUID $uuid UUID of the image
     */
    public function getImageData(MUUID $uuid) {
        // Prepare the sql query
        $sql = 'select data from assets where uuid=?';
        $stmt = $this->prepare($this->db,$sql);

        // Set values
        $stmt->bindValue(1,$uuid->getUnHyphenated());
        $this->execute($stmt);

        // Execute the sql and fetch a the image data
        $result = $this->fetch($stmt);

        // Return the blob
        return $result[0];
    }

    /**
     * Returns the asset with the given uuid
     *
     * @param MUUID $uuid asset uuid
     * @return MAsset 
     */
    public function getAsset(MUUID $uuid)
    {
        // Prepare the sql query
        $sql = 'select uuid,name,description,type,data from assets where uuid=?';
        $stmt = $this->prepare($this->db,$sql);

        // Set values
        $stmt->bindValue(1,$uuid->getUnHyphenated());
        $this->execute($stmt);

        // Execute the sql and fetch a the image data
        $result = $this->fetch($stmt);
        
        // Create new Asset
        $asset = new MAsset($uuid, 
            $result[1], $result[2], $result[3],
            $result[4]);

        // Return the blob
        return $asset;
    }

    /**
     * Add new asset
     *
     * @param MAsset $asset Add new asset
     */
    public function addAsset(MAsset $asset) {
        // Prepare the sql
        $sql = 'insert into assets(uuid,name,description,type,data)' .
                ' values(?,?,?,?,?)';
        $stmt = $this->prepare($this->db,$sql);

        // Set Values
        $counter = 1;
        $stmt->bindValue($counter++,$asset->getUUID()->getUnHyphenated());
        $stmt->bindValue($counter++,$asset->getName());
        $stmt->bindValue($counter++,$asset->getDescription());
        $stmt->bindValue($counter++,$asset->getType());
        $stmt->bindValue($counter++,$asset->getData());

        // Execute the sql
        $this->execute($stmt);
    }
}
?>